package com.sxx.beautifulcity.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.entity.PluckData;
import com.sxx.beautifulcity.entity.greendao.PluckDaoEntity;
import com.sxx.beautifulcity.listenerInterface.OnMyItemClickListener;

import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class PluckAdapter extends RecyclerView.Adapter<PluckAdapter.PluckAdapterViewHolder> {

    private List<PluckDaoEntity> list;
    private OnMyItemClickListener listener;

    public void setOnMyItemClick(OnMyItemClickListener l) {
        listener = l;
    }

    public PluckAdapter(Context context) {

    }

    public void addData(List<PluckDaoEntity> data){
        list = data;
        notifyDataSetChanged();
    }

    class PluckAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private SimpleDraweeView pluckDV;//采摘园图片
        private TextView pluckNameTv;//采摘园名字
        private RatingBar pluckRb;//星评论
        private int position;
        private LinearLayout pluckLinearlayout;
        public PluckAdapterViewHolder(View v) {
            super(v);
            pluckDV = (SimpleDraweeView) v.findViewById(R.id.pluckDV);
            pluckNameTv = (TextView) v.findViewById(R.id.pluckNameTv);
            pluckRb = (RatingBar) v.findViewById(R.id.pluckRb);
            pluckLinearlayout = (LinearLayout) v.findViewById(R.id.pluckLinearlayout);
            pluckLinearlayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (list != null && list.size() > 0) {
                PluckDaoEntity data = list.get(position);
                if (listener != null) {
                    listener.onMyItemClick(data);
                }
            }
        }
    }


    @Override
    public PluckAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.activity_pluck_item, null);

        return new PluckAdapterViewHolder(v);
    }

    @Override
    public void onBindViewHolder(PluckAdapterViewHolder holder, int position) {

        if (list != null && list.size() > 0) {
            PluckDaoEntity data = list.get(position);
            holder.pluckNameTv.setText(data.getName());
            holder.pluckRb.setNumStars(data.getStar());
            String url = data.getImage();
            frescoImage(url , holder.pluckDV);
        }
            holder.position = position;
    }

    @Override
    public int getItemCount() {

        return list != null && list.size() > 0 ? list.size() : 0;
    }

    public void frescoImage(String path , SimpleDraweeView pluckDV){
        Uri uri = Uri.parse(path);
        pluckDV.setImageURI(uri);
    }

}
